<template>
	<view>
		<u-navbar title="我的" :titleStyle="{color: '#fff'}" placeholder leftIconSize="0" :bgColor="appColor"/>
		<scroll-view :scroll-y="true" :style="{height: scrollViewHeight, backgroundColor: '#f5f5f5'}">
			<template v-if="loading">
				<view style="padding: 32rpx;">
					<u-skeleton style="margin-bottom: 32rpx;" :loading="loading" avatar rows="7" v-for="i in 4" :key="i"></u-skeleton>
				</view>
			</template>
			<template v-else>
				<!-- 头部 -->
				<view class="me-header" :style="{backgroundColor: appColor}">
					<view style="height: 32rpx;"></view>
					<view class="me-profile">
						<u--image mode="aspectFill" shape="circle" :src="userInfo.avatar" width="100rpx" height="100rpx"></u--image>
						<view class="me-profile-center">
							<view>{{userInfo.username}}</view>
							<view>{{userInfo.phone}}</view>
						</view>
						<u-icon @click="toPage({url: '/pages/me/setting'})" name="setting-fill" color="#fff" size="28"></u-icon>
					</view>
					<view class="me-moeny">
						<view class="me-moeny-item" @click="toCharge({url: '/pages/me/recharge'})">
							<view class="me-moeny-item-icon">
								<text class="iconfont icon-yue" style="font-weight: bold;color: #fff; size: 32rpx"></text>
								<text style="padding-left: 4rpx;"> 余额</text>
							</view>
							<text class="me-moeny-item-num">&yen;{{userInfo.balance | moneyFormat}}</text>
						</view>
						<view style="width: 32rpx;"/>
						<!-- <view class="me-moeny-item">
							<view class="me-moeny-item-icon">
								<text class="iconfont icon-zijin_dongjie_o" style="font-weight: bold; color: #fff; size: 32rpx"></text>
								<text style="padding-left: 4rpx;"> 冻结</text>
							</view>
							<text class="me-moeny-item-num">&yen;{{userInfo.balance}}</text>
						</view> -->
						<view style="width: 32rpx;"/>
						<view class="me-moeny-item" @click="toPage({url: '/pages/me/points'})">
							<view class="me-moeny-item-icon">
								<text class="iconfont icon-jifen" style="color: #fff; size: 32rpx"></text>
								<text style="padding-left: 4rpx;"> 积分</text>
							</view>
							<text class="me-moeny-item-num">{{userInfo.score}}</text>
						</view>
					</view>
					<view class="me-header-vip">
						<u-icon name="integral" color="#000" size="32"></u-icon>
						<text class="me-header-vip-1">普通会员</text>
						<view class="me-header-vip-text">会员升级, 享受超级权益</view>
					</view>
				</view>
				<!-- 内容 -->
				<view style="height: 64rpx;"/>
				<view class="me-content">
					<view class="me-content-item">
						<view class="me-content-item-title">
							<text class="me-content-item-text">我的订单</text>
							<view class="me-content-item-right" @click="toPage({url: '/pages/me/shopOrder?status=0'})">
								<text>查看所有订单</text>
								<u-icon name="arrow-right" color="#666" size="13"></u-icon>
							</view>
						</view>
						<view class="me-content-item-content-box">
							<view @click="toPage(tag)" class="me-content-item-content" v-for="(tag, index) in ddList" :key="index">
								<u-badge :offset="[-3, -3]" absolute :bgColor="appColor" max="99" :value="orderCount[tag.key]"></u-badge>
								<text :class="['iconfont', tag.icon]" style="color: #666; font-size: 44rpx"></text>
								<text>{{tag.title}}</text>
							</view>
						</view>
					</view>
					<view class="me-content-item">
						<view class="me-content-item-title" @click="toPage({url: '/pages/me/fosterOrder'})">
							<text class="me-content-item-text">我的认养</text>
							<view class="me-content-item-right">
								<text>全部</text>
								<u-icon name="arrow-right" color="#666" size="13"></u-icon>
							</view>
						</view>
						<view v-if="adoping.id" class="me-content-item-content-image" @click="toPage({url: `/pages/me/fosterOrderDetail?id=${adoping.id}&index=0`})">
							<u--image radius="8rpx" mode="aspectFill" :src="adoping.adoptProject.image[0].url" width="156rpx" height="156rpx"></u--image>
							<view class="me-content-item-content-image-r">
								<view style="display: flex;">
									<text style="font-size: 32rpx;color: #333;font-weight: bold;">{{adoping.adoptProject.name}}</text>
									<view class="r-tag" :style="{color: appColor, borderColor: appColor}">第{{adoping.adoptProject.numbers}}期</view>
								</view>
								<view>
									<text>数量:</text>
									<text class="r-1">{{adoping.buyCount}}只</text>
								</view>
								<view>
									<text>认养状态:</text>
									<text class="r-1">认养中</text>
								</view>
							</view>
						</view>
						<view class="me-content-item-content-box">
							<view @click="toPage({url: tag.url ? tag.url : adoping.id ? `/pages/me/fosterOrderDetail?id=${adoping.id}&index=${index}` : '/pages/me/fosterOrder'})" class="me-content-item-content" v-for="(tag, index) in ryList" :key="index">
								<text :class="['iconfont', tag.icon]" style="color: #666; font-size: 44rpx"></text>
								<text>{{tag.title}}</text>
							</view>
						</view>
					</view>
					<view class="me-content-item">
						<view class="me-content-item-title">
							<text class="me-content-item-text">辅助功能</text>
						</view>
						<view class="me-content-item-content-box">
							<view @click="toPage(tag)" class="me-content-item-content" v-for="(tag, index) in fzList" :key="index">
								<text :class="['iconfont', tag.icon]" style="color: #666; font-size: 44rpx"></text>
								<text>{{tag.title}}</text>
							</view>
						</view>
					</view>
					<view class="me-content-item">
						<view class="me-content-item-title">
							<text class="me-content-item-text">商家必备</text>
						</view>
						<view class="me-content-item-content-box">
							<view @click="toPage(tag)" class="me-content-item-content" v-for="(tag, index) in sjList" :key="index">
								<text :class="['iconfont', tag.icon]" style="color: #666; font-size: 44rpx"></text>
								<text>{{tag.title}}</text>
							</view>
						</view>
					</view>
				</view>
			</template>
		</scroll-view>
	</view>
</template>

<script>
	import {mapState, mapActions} from 'vuex';
	import {queryAccountById, adoptOrderList, getOrderCount} from '@/api/me.js'
	import {getToken} from '@/utils/auth.js';
	export default {
		data() {
			return {
				loading: true,
				// 订单icon
				ddList: [
					{title: '待支付', icon: 'icon-daizhifu', url: '/pages/me/shopOrder?status=1', key: 'unPay'},
					{title: '待发货', icon: 'icon-baoguo_fahuo_o', url: '/pages/me/shopOrder?status=2', key: 'deliver'},
					{title: '待确认', icon: 'icon-baoguo_shouhuo_o', url: '/pages/me/shopOrder?status=3', key: 'confirm'},
					{title: '已完成', icon: 'icon-chuanshuwancheng', url: '/pages/me/shopOrder?status=4', key: 'complete'},
					{title: '售后', icon: 'icon-shouhoufuwu', url: '/pages/me/afterSale', key: 'afterSale'},
				],
				// 认养icon
				ryList: [
					{title: '基本信息', icon: 'icon-kufangguanli-wuliaoguanli', url: ''},
					{title: '视频监控', icon: 'icon-jiankong', url: ''},
					{title: '认养日志', icon: 'icon-gxunjians', url: ''},
					{title: '配送信息', icon: 'icon-peisong2', url: ''},
					{title: '售后', icon: 'icon-shouhou', url: '/pages/me/afterSaleFoster'},
				],
				// 辅助icon
				fzList: [
					{title: '收货地址', icon: 'icon-shouhuodizhi', url: '/pages/me/address'},
					// {title: '专属客服', icon: 'icon-kefufill', url: ''},
					{title: '优惠券', icon: 'icon-youhuiquan', url: '/pages/me/myCoupon'},
					{title: '去领券', icon: 'icon-quan', url: '/pages/me/coupon'},
					// {title: '签到', icon: 'chat-fill', url: ''},
					{title: '常见问题', icon: 'icon-changjianwentixiangguanwenti2', url: '/pages/me/question'},
				],
				// 商家必备icon
				sjList: [
					{title: '认养工单', icon: 'icon-gongdan1', url: '/pages/me/plan'},
				],
				adoping: {}
			}
		},
		computed: {
			...mapState(['userInfo', 'orderCount', 'appInfo']),
			scrollViewHeight() {
				return this.scrollHeight - 50 - 1 - 44 + 'px';
			},
			appColor() {
				return this.appPrimaryColor
			},
		},
		created() {
			this.loading = true;
			// 更新用户信息
			queryAccountById({id: this.userInfo.id}).then(res => {
				this.setUserInfo({
					userInfo: res.result,
					token: getToken()
				})
			})
			// 查询认养
			adoptOrderList({pageSize: 1, pageNo: 1, orderStatus: 2}).then(res => {
				this.adoping = res.result.records[0] || {}
			}).finally(() => {
				setTimeout(() => {
					this.loading = false;
				}, 200)
			});
			// 查询订单状态数量
			getOrderCount().then(res => {
				this.setOrderCount({orderCount: res.result});
			})
		},
		methods: {
			...mapActions(['setUserInfo', 'setOrderCount']),
			toPage({url, redirectTo}) {
				if(!url) return
				this.$cuNavigateTo(url);
			},
			// 充值
			toCharge({url}) {
				const {rechargeIsOpen} = this.appInfo;
				console.log(this.appInfo);
				if(rechargeIsOpen === '0') {
					return this.$cuTip('充值关闭');
				}
				this.toPage({url});
			}
		}
	}
</script>


<style lang="scss" scoped>
	.me-content {
		padding: 0 32rpx;
		.me-content-item {
			padding: 32rpx;
			background-color: #fff;
			border-radius: 16rpx;
			margin-bottom: 32rpx;
			
			.me-content-item-title {
				display: flex;
				align-items: center;
				justify-content: space-between;
				.me-content-item-text {
					font-size: 32rpx;
					color: #333;
					font-weight: bold;
				}
				.me-content-item-right {
					font-size: 24rpx;
					color: #666;
					display: flex;
					align-items: center;
				}
			}
			.me-content-item-content-image {
				display: flex;
				align-items: center;
				height: 156rpx;
				padding-top: 32rpx;
				.me-content-item-content-image-r {
					padding-left: 16rpx;
					height: 100%;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					font-size: 26rpx;
					color: #666;
					.r-tag {
						font-size: 28rpx;
						padding: 0 12rpx;
						margin-left: 32rpx;
						border-radius: 8rpx;
						border-width: 2rpx;
						border-style: solid;
					}
					.r-1 {
						color: #333;
						padding-left: 16rpx;
					}
				}
			}
			.me-content-item-content-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 16rpx 16rpx 0 16rpx;
				.me-content-item-content {
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: #333;
					position: relative;
				}
			}
			
			.me-content-item-content-box1 {
				flex-wrap: wrap;
				justify-content: flex-start;
				padding-top: 0;
				.me-content-item-content {
					width: 25%;
					padding-top: 32rpx;
				}
			}
		}
	}
	
	.me-header {
		padding: 0 32rpx;
		height: 400rpx;
		.me-profile {
			display: flex;
			align-items: center;
			justify-content: space-between;
			.me-profile-center {
				flex: 1;
				padding-left: 16rpx;
				color: #fff;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
		.me-moeny {
			display: flex;
			color: #fff;
			padding: 32rpx 0;
			.me-moeny-item {
				padding: 16px 0;
				background-color: rgba(255, 255, 255, 0.2);
				border-radius: 16rpx;
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				.me-moeny-item-icon {
					display: flex;
					align-items: center;
				}
				.me-moeny-item-num {
					font-size: 32rpx;
				}
			}
		}
		.me-header-vip {
			height: 96rpx;
			background-color: rgba(245, 214, 156, 1);
			border-radius: 16rpx;
			display: flex;
			align-items: center;
			padding: 0 64rpx;
			box-shadow: 0 0 6px 6px rgba(0, 0, 0, 0.1);
			.me-header-vip-1 {
				padding: 0 16rpx;
				color: #333;
				font-weight: bold;
			}
			.me-header-vip-text {
				font-size: 28rpx;
				color: #333;
			}
		}
	}
</style>
